﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.DomainObject.Administration.UserManagement;
using SIPO.Library.DomainObject.Transaction;
using SIPO.Library.DataAccess.Transaction;
using SIPO.Library.DataAccess.Administration.Catalog;
using SIPO.Library.DataAccess.Administration.UserManagement;
using System.Configuration;

namespace SIPO.Web.forms.Transaction
{
    public partial class recAcceptanceform : BasePage
    {
        private int id = 0;
        private bool isView = false;
        private bool isApproved = false;

        protected override void OnLoad(EventArgs e)
        {
            if (Request.Params["id"] != null)
            {
                id = Convert.ToInt32(Request.Params["id"]);
            }

            if (Request.Params["view"] != null)
            {
                isView = Request.Params["view"] == "1" ? true : false;
            }

            if (Request.Params["review"] != null)
            {
                isApproved = Request.Params["review"] == "1" ? true : false;
            }

            if (!IsPostBack)
            {
                hfAcceptanceId.Value = id.ToString();

                int currentTahunLapKeuYear = DateTime.Now.Year;
                int nextYear = DateTime.Now.Year + 11;

                IList<string> collTahunLapKeuYears = new List<string>();
                collTahunLapKeuYears.Add("");
                for (int i = currentTahunLapKeuYear; i < nextYear; i++)
                {
                    collTahunLapKeuYears.Add(i.ToString());
                }

                ddlPeriode.DataSource = collTahunLapKeuYears;
                ddlPeriode.DataBind();

                if (id > 0)
                {
                    recAcceptanceRepo acceptanceDal = new recAcceptanceRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                    recAcceptance acceptance = acceptanceDal.GetById(id);

                    if (acceptance != null)
                    {
                        if (acceptance.AcceptancePeriod > 0)
                        {
                            ddlPeriode.Items.FindByValue(acceptance.AcceptancePeriod.ToString());
                            txtPeriode.Value = acceptance.AcceptancePeriod.ToString();
                        }

                        txtNoSk.Text = acceptance.SkNo;
                        txtTglSk.Text = acceptance.SkDate.ToString();

                        if (isView || isApproved)
                            txtVersi.Value = acceptance.Versi.HasValue ? acceptance.Versi.Value.ToString() : "1";
                        else
                            txtVersi.Value = acceptance.Versi.HasValue ? (acceptance.Versi.Value + 1).ToString() : "1";
                    }
                }

            }
        }
    }
}